package com.weijie.relaxed.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.weijie.relaxed.R;

/** 
 * 输入框 
 * @author weijie 
 * @version 创建时间：2015年3月18日 下午4:42:03 
 */
public class InputView extends LinearLayout {

	private EditText edit;
	private ImageView lIcon,rIcon;
	
	public InputView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.relaxed, 0, 0);
		String hint = ta.getString(R.styleable.relaxed_input_hint);
		Drawable left = ta.getDrawable(R.styleable.relaxed_input_left_icon);
		Drawable right = ta.getDrawable(R.styleable.relaxed_input_right_icon);
		int inputType = ta.getInteger(R.styleable.relaxed_input_type, 0);
		ta.recycle();
		inflate(context, R.layout.view_input_field, this);
		edit = (EditText) findViewById(R.id.edit);
		lIcon = (ImageView) findViewById(R.id.left);
		if(hint!=null) {
			edit.setHint(hint);
		}
		if(left!=null) {
			lIcon.setImageDrawable(left);
		}
		switch (inputType) {
		case 1:
			edit.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
			break;
		case 2:
			edit.setInputType(EditorInfo.TYPE_CLASS_NUMBER
					| EditorInfo.TYPE_NUMBER_FLAG_SIGNED);
			break;
		case 3:
			edit.setInputType(EditorInfo.TYPE_CLASS_NUMBER
					| EditorInfo.TYPE_NUMBER_FLAG_DECIMAL);
			break;
		case 4:
			edit.setInputType(EditorInfo.TYPE_CLASS_TEXT
					| EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
			break;
		case 5:
			edit.setInputType(EditorInfo.TYPE_CLASS_NUMBER
					| EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD);
			break;
		case 6:
			edit.setInputType(EditorInfo.TYPE_CLASS_PHONE);
			break;
		default:
			edit.setInputType(EditorInfo.TYPE_CLASS_TEXT);
			break;
		}
	}
	
	public String getText() {
		return edit.getText().toString();
	}
	
}
